﻿@* @inject IProductionResourceService ProductionResourceService

<MudDialog>
    <DialogContent>
        <MudForm @ref="_editRequestForm" Model="_editRequestDto" Validation="@(_editValidator.ValidateValue)" ValidationDelay="0">
            <MudGrid>
                <MudItem xs="12" md="6">
                    <MudTextField @bind-Value="_editRequestDto.Code" For="(() => _editRequestDto.Code)"
                                  Disabled="@(Key > 0)" Variant="Variant.Outlined" Margin="Margin.Dense"
                                  Label="生产资源编码">
                    </MudTextField>
                </MudItem>
                <MudItem xs="12" md="6">
                    <MudTextField @bind-Value="_editRequestDto.Name" For="(() => _editRequestDto.Name)"
                                  Disabled="@(Key > 0)" Variant="Variant.Outlined" Margin="Margin.Dense"
                                  Label="生产资源名称">
                    </MudTextField>
                </MudItem>
                <MudItem xs="12" md="6">
                    <MudSelect @bind-Value="_editRequestDto.ResourceCategory" For="(() => _editRequestDto.ResourceCategory)"
                               Label="资源类别" AnchorOrigin="Origin.BottomCenter"
                               Variant="Variant.Outlined" Margin="Margin.Dense" Dense="true">
                        <MudSelectItem T="int?" Value="1">机器</MudSelectItem>
                        <MudSelectItem T="int?" Value="2">人员</MudSelectItem>
                        <MudSelectItem T="int?" Value="3">外发</MudSelectItem>
                    </MudSelect>
                </MudItem>
            </MudGrid>
        </MudForm>
    </DialogContent>
    <DialogActions>
        <MudButton Variant="Variant.Filled" Color="Color.Primary" OnClick="SubmitHandle">保存</MudButton>
        <MudButton Variant="Variant.Filled" OnClick="CancelHandle">取消</MudButton>
    </DialogActions>
</MudDialog>

@code {
    private ProductionResourceEditRequestDto _editRequestDto = new();
    private ProductionResourceEditValidator _editValidator = new();
    private MudForm _editRequestForm;

    [CascadingParameter]
    MudDialogInstance MudDialog { get; set; }

    [Parameter]
    public int Key { get; set; }

    void CancelHandle()
    {
        MudDialog.Cancel();
    }

    async Task SubmitHandle()
    {
        await _editRequestForm.Validate();

        if (Key > 0)
        {
            var result = await ProductionResourceService.UpdateProductionResource(Key, _editRequestDto);

            if (result.Succeeded)
            {
                MudDialog.Close(DialogResult.Ok(true));
            }
        }
        else
        {
            var result = await ProductionResourceService.CreateProductionResource(_editRequestDto);

            if (result != null && result.Succeeded)
            {
                MudDialog.Close(DialogResult.Ok(true));
            }
        }
    }

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();
    }

    protected override async Task OnParametersSetAsync()
    {
        _editRequestDto = new ProductionResourceEditRequestDto();
        if (Key > 0)
        {
            var result = await ProductionResourceService.GetProductionResource(Key);
            if (result != null && result.Succeeded && result.Data != null)
            {
                _editRequestDto.Code = result.Data.Code;
                _editRequestDto.Name = result.Data.Name;
                _editRequestDto.Description = result.Data.Description;
            }
        }
    }

} *@